//
//  TrackpadViewController.m
//  McRemote
//
//  Created by Magic on 2/9/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "TrackpadViewController.h"
#import "MRMessageCenter.h"

@interface TrackpadViewController () 

- (void)playTouchEffectAtPoint:(CGPoint)p;



@end

@implementation TrackpadViewController

@synthesize touchIndicator;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView
 {
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    tapRec.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:tapRec];
    
    UITapGestureRecognizer *twoFingersTapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingersTap:)];
    twoFingersTapRec.numberOfTouchesRequired = 2;
    [self.view addGestureRecognizer:twoFingersTapRec];
    
    //    UITapGestureRecognizer *doubleTapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    //    doubleTapRec.numberOfTouchesRequired = 1;
    //    doubleTapRec.numberOfTapsRequired = 2;
    //    [self.view addGestureRecognizer:doubleTapRec];
    
    UIPanGestureRecognizer *panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [panRec setMinimumNumberOfTouches:1];
    [self.view addGestureRecognizer:panRec];
    
    
//    UIPanGestureRecognizer *twoFingersPanRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingersPan:)];
//    [twoFingersPanRec setMinimumNumberOfTouches:2];
//    [self.view addGestureRecognizer:twoFingersPanRec];
}

- (void)handleTwoFingersPan:(UIPanGestureRecognizer *)gestureRecognizer
{
    //static BOOL first = YES;
    static CGPoint previous = {0,0};
    static int counter = 0;
    static BOOL first = YES;
    
    if (gestureRecognizer.state == UIGestureRecognizerStateChanged)     
    { 
        if (first) {
            first = NO;
            [[MRMessageCenter sharedCenter] sendWheelScrollWithOffset:CGPointMake(73822, 81345)]; //magic number :D
        }
        
        if (counter++ >= 3) {
            counter = 0;
        
        CGPoint p = [gestureRecognizer translationInView:self.view];
        
        //CGFloat offsetX = p.x - previous.x;
        //CGFloat offsetY = p.y - previous.y;
        
        //if (offsetX > 5.0f || offsetX < -5.0f || offsetY > 5.0f || offsetY < -5.0f) {
            
            NSLog(@"twoFingersPan(%f,%f)",p.x,p.y);
            [[MRMessageCenter sharedCenter] sendWheelScrollWithOffset:p];
            previous = p;
       // }
        
        
        }
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded)     
    {
        CGPoint location = [gestureRecognizer locationInView:self.view];
        first = YES;
        [self playTouchEffectAtPoint:location];
        //[[MRMessageCenter sharedCenter] sendMouseMoveWithOffset:CGPointMake(73822, 81345)];
    }
}

- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
    //static CGPoint last = {0,0};
    static int counter = 0;
    static BOOL first = YES;
    static CGPoint previous = {0,0};
    
    
    if (gestureRecognizer.state == UIGestureRecognizerStateChanged)     
    { 
        if (first) {
            first = NO;
            [[MRMessageCenter sharedCenter] sendMouseMoveWithOffset:CGPointMake(73822, 81345)]; //magic number :D
        }
        CGPoint p = [gestureRecognizer translationInView:self.view];
        if (counter++ >= 1) {
            counter = 0;
        CGFloat offsetX = p.x - previous.x;
        CGFloat offsetY = p.y - previous.y;
        
        if (offsetX > 1.0f || offsetX < -1.0f || offsetY > 1.0f || offsetY < -1.0f) {
            NSLog(@"pan(%f,%f)",p.x,p.y);
   
            [[MRMessageCenter sharedCenter] sendMouseMoveWithOffset:p];
            
            previous = p;
        }
            
            
            
        }
              
        
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) 
    {
        //first = YES;
        
        //[[MRMessageCenter sharedCenter] sendMouseMoveWithOffset:CGPointMake(73822, 81345)];
        
        
    }  
    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded)     
    {
        CGPoint location = [gestureRecognizer locationInView:self.view];
        first = YES;
        [self playTouchEffectAtPoint:location];
        //[[MRMessageCenter sharedCenter] sendMouseMoveWithOffset:CGPointMake(73822, 81345)];
    }
    
}

//- (void)handleDoubleTap:(UITapGestureRecognizer *)gestureRecognizer
//{
//    if (gestureRecognizer.state == UIGestureRecognizerStateEnded)     
//    { 
//        NSLog(@"double");
//    }
//    
//}

- (void)handleTwoFingersTap:(UITapGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded)     
    { 
        NSLog(@"two finger tap");
        CGPoint p = [gestureRecognizer locationInView:self.view];
        [self playTouchEffectAtPoint:p];
        
        [[MRMessageCenter sharedCenter] sendRightMouseClick];
    }
}

- (void)handleTap:(UITapGestureRecognizer *)gestureRecognizer
{
    
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded)     
    {         
        NSLog(@"tap");
        CGPoint p = [gestureRecognizer locationInView:self.view];
        [self playTouchEffectAtPoint:p];
        
        [[MRMessageCenter sharedCenter] sendLeftMouseClick];
    }
      
}

- (void)playTouchEffectAtPoint:(CGPoint)p
{     
    self.touchIndicator.center = p;
    self.touchIndicator.alpha = 1;
    self.touchIndicator.transform = CGAffineTransformMakeScale(0.1, 0.1);
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    self.touchIndicator.transform = CGAffineTransformIdentity;
    self.touchIndicator.alpha = 0;
    
    [UIView commitAnimations];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}



@end
